package com.business.action.system;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import com.business.bean.system.DepartmentBean;
import com.business.bean.system.UserBean;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import net.sf.rose.jdbc.DBUtils;
import net.sf.rose.jdbc.PageBean;
import net.sf.rose.jdbc.dao.BeanDAO;
import net.sf.rose.jdbc.query.BeanSQL;
import net.sf.rose.jdbc.service.Service;
import net.sf.rose.util.DateFormat;
import net.sf.rose.web.util.WebUtils;

/**
 * 用户管理
 */
@Controller
@RequestMapping("/user")
public class UserAction {

	/**
	 * 用户登录
	 * 
	 * @author fengjian
	 * @since 2019/09/21
	 */
	@ResponseBody
	@RequestMapping("/any/login.do")
	public PageBean login(HttpServletRequest request, Service service, UserBean bean) {
		PageBean page = new PageBean();
		BeanDAO dao = new BeanDAO(service);
		if (bean.getLoginName() == null) {
			page.setSuccess(false);
			page.setMessage("用户登录名不能为空");
			return page;
		} else if (bean.getLoginPassword() == null) {
			page.setSuccess(false);
			page.setMessage("用户密码不能为空");
			return page;
		}
		BeanSQL query = dao.getQuerySQL();
		query.setEntityClass(UserBean.class);
		query.createSql(bean);
		query.setSQL("select * from SYS_USER where status=1 and loginName=?");
		query.addParameter(bean.getLoginName());
		UserBean user = dao.get();
		if (user == null) {
			page.setSuccess(false);
			page.setMessage("用户名不存在！");
			return page;
		}
		if (!user.getLoginPassword().equals(bean.getLoginPassword())) {
			page.setSuccess(false);
			page.setMessage("密码错误！");
			return page;
		}
		// 登录成功，添加该用户到session
		String token = WebUtils.setUserBeanCache(request, user);
		page.getDataMap().put("_token", token);
		// 增加登录次数，记录最新登录时间
		UserBean u = new UserBean();
		u.setUserID(user.getUserID());
		u.setLoginCount(user.getLoginCount() + 1);
		u.setLastLoginTime(DateFormat.getTimestamp());
		query.createUpdateSql(u);
		dao.update();
		page.setData(user);
		return page;
	}

	/**
	 * 微信登录
	 * 
	 * @author fengjian
	 * @since 2019/09/21
	 */
	@ResponseBody
	@RequestMapping("/loginByOpenId.do")
	public PageBean loginByOpenId(HttpServletRequest request, Service service, String openId) {
		PageBean page = new PageBean();
		BeanDAO dao = new BeanDAO(service);
		BeanSQL query = dao.getQuerySQL();
		query.setEntityClass(UserBean.class);
		query.createSql("openId", openId);
		UserBean user = dao.get();
		if (user == null) {
			user = new UserBean();
			user.setOpenId(openId);
			user.setUserName("游客");
		} else {
			// 增加登录次数，记录最新登录时间
			UserBean u = new UserBean();
			u.setUserID(user.getUserID());
			u.setLoginCount(user.getLoginCount() + 1);
			u.setLastLoginTime(DateFormat.getTimestamp());
			query.createUpdateSql(u);
			dao.update();
		}
		// 登录成功，添加该用户到session
		String token = WebUtils.setUserBeanCache(request, user);
		page.getDataMap().put("_token", token);
		page.setData(user);
		return page;
	}

	/**
	 * 修改密码
	 * 
	 * @author fengjian
	 * @since 2019/09/21
	 */
	@ResponseBody
	@RequestMapping("/changePassword.do")
	public PageBean changePassword(HttpServletRequest request, Service service) {
		PageBean page = new PageBean();
		BeanDAO dao = new BeanDAO(service);
		BeanSQL query = dao.getQuerySQL();
		String oldPass = request.getParameter("oldPassword");
		String pass = request.getParameter("loginPassword");
		String userID = request.getParameter("userID");
		query.setEntityClass(UserBean.class);
		query.createSql(userID);
		UserBean u = dao.get();
		if (u == null) {
			page.setSuccess(false);
			page.setMessage("用户不存在！");
			return page;
		} else if (!u.getLoginPassword().equals(oldPass)) {
			page.setSuccess(false);
			page.setMessage("旧密码不正确！");
			return page;
		}
		UserBean bean = new UserBean();
		bean.setUserID(userID);
		bean.setLoginPassword(pass);
		query.createSaveSql(bean);
		if (dao.update() != 1) {
			page.setSuccess(false);
			page.setMessage("没有修改成功！");
		}
		return page;
	}

	/**
	 * 分页查询
	 * 
	 * @author fengjian
	 * @since 2019/09/21
	 */
	@ResponseBody
	@RequestMapping("/page.do")
	public PageBean page(HttpServletRequest request, Service service) {
		PageBean page = WebUtils.getPageBean(request);
		Map<String, Object> map = WebUtils.getRequestData(request);
		BeanDAO dao = new BeanDAO(service);
		BeanSQL query = dao.getQuerySQL();
		query.setPage(page);
		query.setEntityClass(UserBean.class);
		query.setTableName("SYS_USER_VIEW");
		query.createSql(map);
		dao.list();
		return page;
	}

	/**
	 * 获取用户所有信息
	 * 
	 * @author fengjian
	 * @since 2019/09/21
	 */
	@ResponseBody
	@RequestMapping("/get.do")
	public PageBean get(Service service, UserBean user) {
		PageBean page = new PageBean();
		BeanDAO dao = new BeanDAO(service);
		BeanSQL query = dao.getQuerySQL();
		query.setEntityClass(UserBean.class);
		query.createSql(user);
		page.setData(dao.get());
		return page;
	}

	/**
	 * 保存用户信息
	 * 
	 * @author fengjian
	 * @since 2019/09/21
	 */
	@ResponseBody
	@RequestMapping("/save.do")
	public PageBean save(Service service, UserBean bean) {
		PageBean page = new PageBean();
		BeanDAO dao = new BeanDAO(service);
		BeanSQL query = dao.getQuerySQL();
		query.setEntityClass(UserBean.class);
		// 如果是更新
		if (bean.getUserID() != null) {
			query.createSql("userID", bean.getUserID());
			UserBean user = dao.get();
			// 如果工号变更了
			if (!user.getWorkNo().equals(bean.getWorkNo())) {
				// TODO
			}
			// 如果登录名变更了
			if (!user.getLoginName().equals(bean.getLoginName())) {
				query.createSql("loginName", bean.getLoginName());
				user = dao.get();
				if (user != null) {
					page.setSuccess(false);
					page.setMessage("登录名(" + bean.getLoginName() + ")已经存在！");
					return page;
				}
			}
			// 密码不能变更
			bean.setLoginPassword(null);
		}
		// 如果是新增
		else {
			// 工号不能重复
			query.createSql("workNo", bean.getWorkNo());
			UserBean user = dao.get();
			if (user != null) {
				page.setSuccess(false);
				page.setMessage("工号(" + bean.getWorkNo() + ")已经存在！");
				return page;
			}
			// 登录名不能重复
			query.createSql("loginName", bean.getLoginName());
			user = dao.get();
			if (user != null) {
				page.setSuccess(false);
				page.setMessage("登录名(" + bean.getLoginName() + ")已经存在！");
				return page;
			}
		}
		if (dao.update() > 0) {
			page.setMessage("保存信息成功！");
		} else {
			page.setSuccess(false);
			page.setMessage("保存信息失败！");
		}
		return page;
	}

	/**
	 * 批量更新
	 * 
	 * @author fengjian
	 * @since 2019/09/21
	 */
	@ResponseBody
	@RequestMapping("/batchUpdate.do")
	public PageBean batchUpdate(Service service, String ids, UserBean bean) {
		PageBean res = new PageBean();
		BeanDAO dao = new BeanDAO(service);
		BeanSQL query = dao.getQuerySQL();
		query.createBatchUpdateSql(bean, ids);
		dao.batchUpdate();
		if (dao.getBatchUpdateRecordCount() > 0) {
			res.setMessage("保存信息成功！");
		} else {
			res.setSuccess(false);
			res.setMessage("保存信息失败！");
		}
		return res;
	}

	/**
	 * 删除
	 * 
	 * @author fengjian
	 * @since 2019/09/21
	 */
	@ResponseBody
	@RequestMapping("/delete.do")
	public PageBean delete(Service service, String userID) {
		return DBUtils.delete(service, UserBean.class, userID);
	}

	/**
	 * 微信绑定工号
	 * 
	 * @author fengjian
	 * @since 2019/09/21
	 */
	@ResponseBody
	@RequestMapping("/addWeixinToWorkNo.do")
	public PageBean addWeixinToWorkNo(Service service, UserBean user) {
		PageBean res = new PageBean();
		BeanDAO dao = new BeanDAO(service);
		BeanSQL query = dao.getQuerySQL();
		String mobile = user.getMobile();
		String openId = user.getOpenId();
		if (user.getWorkNo() == null || user.getUserName() == null || mobile == null) {
			res.setSuccess(false);
			res.setMessage("姓名、手机号码和工号不能为空!");
			return res;
		}
		if (openId == null) {
			res.setSuccess(false);
			res.setMessage("微信标识不能为空!");
			return res;
		}
		user.setMobile(null);
		user.setOpenId(null);
		query.setEntityClass(UserBean.class);
		query.createSql(user);
		// query.setSQL("selecct * from SYS_USER where workNo=? and userName=?");
		// query.addParameter(user.getWorkNo());
		// query.addParameter(user.getUserName());
		user = dao.get();
		if (user == null) {
			res.setSuccess(false);
			res.setMessage("根据姓名和工号没有找到员工数据!");
			return res;
		}
		UserBean bean = new UserBean();
		bean.setUserID(user.getUserID());
		bean.setMobile(mobile);
		bean.setOpenId(openId);
		query.createUpdateSql(bean);
		if (dao.update() > 0) {
			res.setMessage("绑定成功！");
		} else {
			res.setMessage("绑定失败！");
			res.setSuccess(false);
		}
		return res;
	}

	/**
	 * 导入
	 */
	@ResponseBody
	@RequestMapping("/input.do")
	public PageBean input(HttpServletRequest request, Service service) {
		PageBean res = new PageBean();
		service.setCurrentDatabaseName("attend");
		BeanDAO dao = new BeanDAO(service);
		BeanSQL query = dao.getQuerySQL();
		query.setSQL(service.getSql("attend.user.select"));
		query.addSql(" where t1.dt_leave_date is null ORDER BY t1.ng_id");
		query.setEntityClass(UserBean.class);
		List<UserBean> list = dao.list();
		list.remove(0); // 删除admin帐号
		service.setCurrentDatabaseName("wison");
		query.setEntityClass(DepartmentBean.class);
		query.createSql();
		query.setEntityClass(UserBean.class);
		query.createBatchInsertSql(list);
		dao.batchUpdate();
		return res;
	}

	/**
	 * @description 根据部门查询用户
	 * @author  jiangwenfeng
	 * @date 创建时间：2019/10/12 21:41
	 */
	@ResponseBody
	@RequestMapping("/getUserListByDepart.do")
	public List<UserBean> getUserListByDepart(HttpServletRequest request, Service service) {
		Map<String, Object> map = WebUtils.getRequestData(request);
		BeanDAO dao = new BeanDAO(service);
		BeanSQL query = dao.getQuerySQL();
		query.setEntityClass(UserBean.class);
		query.createSql(map);
		return dao.list();
	}

	/**
	 * @description 根据部门查询用户
	 * @author  jiangwenfeng
	 * @date 创建时间：2019/10/12 21:41
	 */
	@ResponseBody
	@RequestMapping("/getUserByMap.do")
	public UserBean getUserByMap(HttpServletRequest request, Service service) {
		Map<String, Object> map = WebUtils.getRequestData(request);
		BeanDAO dao = new BeanDAO(service);
		BeanSQL query = dao.getQuerySQL();
		query.setEntityClass(UserBean.class);
		query.setTableName("SYS_USER_VIEW");
		query.createSql(map);
		return dao.get();
	}

}
